/* GUI CONVERTED from gppibx03.p (converter v1.76) Fri Dec 13 04:04:17 2002 */
/* gppibx03.p - CONSIDER USING A PRICE LIST                             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.21 $                                                         */
/*V8:ConvertMode=Maintenance                                            */
/* REVISION: 8.5      LAST MODIFIED: 12/27/95   BY: afs *J042*          */
/* REVISION: 8.5      LAST MODIFIED: 08/08/95   BY: DAH *J063*          */
/* REVISION: 8.5      LAST MODIFIED: 03/28/96   BY: mzh *J0GK*          */
/* REVISION: 8.5      LAST MODIFIED: 04/05/96   BY: DAH *J0H8*          */
/* REVISION: 8.5      LAST MODIFIED: 07/05/96   BY: DAH *J0XR*          */
/* REVISION: 8.5      LAST MODIFIED: 07/19/96   BY: DAH *J0ZW*          */
/* REVISION: 8.5      LAST MODIFIED: 04/08/97   BY: *J1N4* Aruna Patil  */
/* REVISION: 8.5      LAST MODIFIED: 09/19/97   BY: *J20X* Ajit Deodhar */
/* REVISION: 8.6E     LAST MODIFIED: 05/09/98   BY: *L00Y* Jeff Wootton */
/* REVISION: 8.6E     LAST MODIFIED: 06/01/98   BY: *J2JJ* Niranjan R.  */
/* REVISION: 8.6E     LAST MODIFIED: 06/12/98   BY: *L01S* Russ Witt    */
/* REVISION: 9.0      LAST MODIFIED: 12/22/98   BY: *M02W* Luke Pokic   */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan   */
/* REVISION: 9.1      LAST MODIFIED: 06/08/99   BY: *J3DW* Mugdha Tambe */
/* REVISION: 9.1      LAST MODIFIED: 04/25/00   BY: *N0CD* Santosh Rao  */
/* REVISION: 9.1      LAST MODIFIED: 08/13/00   BY: *N0KS* myb          */
/* REVISION: 9.1      LAST MODIFIED: 03/12/01   BY: *L16Y* Nikita Joshi */
/* Revision: 1.18     BY: Karan Motwani         DATE: 09/12/02  ECO: *N1TL*  */
/* Revision: 1.19     BY: Nikita Joshi          DATE: 10/11/02  ECO: *M20Y*  */
/* $Revision: 1.21 $    BY: Rajiv Ramaiah         DATE: 12/09/02  ECO: *M21G*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* This routine considers a price list offered up by earlier gppibx*
   selections.

   INPUTS:

   Price list id
   key to wkpi
   item and um
   component identifiers (parent, feature and option) (blank for parent)

   pricing shared variables (?)
   quantity workfile

   OUTPUT:

   A possible addition to the price list used workfile.
   err_flag (These may not be necessary, but they will be interesting)
      0 - price list added to workfile
     -1 - rejected - manual price list failure
     -2 -    "     - minimum order quantity not met
     -3 -    "     - no matching quantity found
     -4 -    "     - qty exceeds pi_max
     -5 -    "     - fails to meet min qty requirements
             (Cannot occur.  Test with 0 qty/amt in this case)
     -6 -    "     - better price of same non-compatible type exists
     -7 -    "     - no conversion to price list UM available
     -8 -    "     - no pt_mstr available for cost mark-up.
     -9 -    "     - 0 cost found for cost mark-up.

   A TYPICAL CALL:

      {gprun.i ""gppibx03.p"" "(pi_list_id,
                sod_part,
                sod_um,
                sob_parent,
                sob_feature,
                sob_option,
                sod_site,
                so_ex_rate,
                so_ex_rate2,  *L00Y*
                err_flag)" }

   FACTORS vs. DISCOUNTS

      Discounts are the way the user things about price reductions
     (i.e 15% off)
      Factors are the mathematical complements of discounts
     (15% off = .85 Factor)

   */
   {mfdeclre.i}

define input        parameter list_id         like pi_list_id.
define input        parameter price_part      like pt_part.
define input        parameter price_um        like pt_um.
define input        parameter price_par       like pih_parent.
define input        parameter price_feat      like pih_feature.
define input        parameter price_opt       like pih_option.
define input        parameter price_site      like sod_site.
define input        parameter price_exch_rate like so_ex_rate.
define input        parameter price_exch_rate2 like so_ex_rate2.
define output       parameter err_flag        as integer.

{pppivar.i}   /* Shared variables for pricing */
{pppiwqty.i}  /* Shared workfile for quantity */
{pppiwkpi.i}  /* Shared workfile for Price Lists used */

define            variable bad_list         like mfc_logical.
define            variable confg_price      as logical.
define            variable conv             as decimal.
define            variable reprice_line     like wrep_line.
define            variable test_cat         like wqty_cat.
define            variable test_factor      as decimal.
define            variable test_price       as decimal.
define            variable test_qty         like wqty_qty.
define            variable test_um          like wqty_um.
define            variable mc-error-number  like msg_nbr  no-undo.
define            variable l_test_price1    as   decimal  no-undo.
define            variable l_test_bk_amt    like wqty_qty no-undo.

for first pi_mstr
   fields(pi_amt_type pi_break_cat pi_comb_type
          pi_confg_disc pi_cost_set pi_disc_seq
          pi_list pi_list_id pi_list_price
          pi_manual pi_max_price pi_max_qty
          pi_min_net pi_min_price pi_qty_type
          pi_srch_type pi_um)
   where pi_list_id = list_id
   no-lock:
end. /* FOR FIRST pi_mstr */

/* If testing a config parent or component, we will be  */
/* sensitive to the global pricing flag.                */

for first pt_mstr
   fields (pt_break_cat pt_fr_class pt_part
           pt_pm_code pt_um)
   where pt_part = price_part
   no-lock:
end. /* FOR FIRST pt_mstr */

confg_price = (available pt_mstr
               and (pt_pm_code = "C" or price_opt <> "")).

/* Secondary validation */

/* Match manual price list */
/* (Currently only one manual price list is valid, and it is */
/* stored in a shared variable (which in turn comes from the */
/* doc header.)                                              */
/* NOW DETERMINED BY LINE, NOT JUST AT THE HEADER.           */
if pi_manual
then do:

   if pi_list <> manual_list
   then do:
      err_flag = -1.
      return.
   end. /* IF pi_list <> manual_list */

end. /* IF pi_manual */

/* List Prices */
if pi_amt_type = "1"
then do:

   test_price = pi_list_price.

   /* Convert price to order UM if necessary */

   if available pt_mstr
      and pi_um = ""
      and pt_um <> price_um
   then do:
      {gprun.i ""gpumcnv.p""
         "(price_um, pt_um, price_part, output conv)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      if conv = ?
      then
         conv = 1.
      test_price = test_price * conv.
   end. /* IF AVAILABLE pt_mstr */
   else
      conv = 1.

   if (test_price < best_system_list)
      or first_pilist
   then do:

      assign
         first_pilist     = no
         best_system_list = test_price .

      /* Assign min/max values (for parents only) */
      if price_opt  = ""
      then
         assign
            min_price = pi_min_price * conv
            max_price = pi_max_price * conv .

      /* Find and delete old record */
      find first wkpi_wkfl
         where wkpi_parent   = price_par
           and wkpi_feature  = price_feat
           and wkpi_option   = price_opt
           and wkpi_amt_type = pi_amt_type
      exclusive-lock no-error.

      /* Do not override manual list price */
      if not available wkpi_wkfl or
         (available wkpi_wkfl and wkpi_source = "0")
      then do:

         if available wkpi_wkfl
         then do:
            /* Create wkpi2_wkfl */
            {gppibx05.i &list=wkpi_list
               &amttype=wkpi_amt_type
               &combtype=wkpi_comb_type
               &breakcat=wkpi_break_cat
               &pidqty=wkpi_pid_qty
               &qtytype=wkpi_qty_type
               &amt=wkpi_amt
               &promo=""""
               }
            delete wkpi_wkfl.
         end. /* IF AVAILABLE wkpi_wkfl */

         /* Create price list record */
         create wkpi_wkfl.
         assign
            wkpi_amt         = test_price
            wkpi_amt_type    = pi_amt_type
            wkpi_break_cat   = pi_break_cat
            wkpi_comb_type   = pi_comb_type
            wkpi_confg_disc  = (not confg_price or pi_confg_disc)
            wkpi_factor      = 0
            wkpi_feature     = price_feat
            wkpi_list        = pi_list
            wkpi_list_id     = pi_list_id
            wkpi_min_net     = pi_min_net
            wkpi_option      = price_opt
            wkpi_parent      = price_par
            wkpi_qty         = test_qty
            wkpi_qty_type    = pi_qty_type
            wkpi_srch_type   = pi_srch_type
            wkpi_source      = "0"
            wkpi_um          = price_um  /*test_um*/
            .

         best_list_price = test_price.

      end. /* IF NOT AVAILABLE wkpi_wkfl */

   end. /* IF (test_price < best_system_list) OR first_pilist */
   else do:
      /* Create wkpi2_wkfl */
      {gppibx05.i &list=pi_list
         &amttype=pi_amt_type
         &combtype=pi_comb_type
         &breakcat=pi_break_cat
         &pidqty=0
         &qtytype=pi_qty_type
         &amt=test_price
         &promo=""""
         }
   end. /* ELSE DO */

end. /* IF pi_amt_type = "1" */

/* Discounts, Terms (which use pid_det) */
else do:

   /* Determine quantity that applies */
   if pi_break_cat <> ""
   then
      test_cat = pi_break_cat.
   else
      test_cat = price_part.

   if pi_um <> ""
   then
      test_um = pi_um.
   else
      test_um = price_um.

   if available pt_mstr
   then
      if  pi_um       = ""
      and pi_qty_type = "1"
      and pt_um       <> price_um
      then
         test_um = pt_um.

   find first wqty_wkfl
      where wqty_cat = test_cat
        and wqty_um  = test_um
      no-lock no-error.

   if not available wqty_wkfl
   then do:

      for first wqty_wkfl
         fields (wqty_cat wqty_ext wqty_qty wqty_um)
         where wqty_cat = test_cat
      no-lock:
      end. /* FOR FIRST wqty_wkfl */
   end. /* IF NOT AVAILABLE wqty_wkfl */

   /* Use qty zero if no record found (it could happen) */
   if not available wqty_wkfl
   then
      test_qty = 0.
   /* Convert quantity to amount if necessary */
   else
   if pi_qty_type = "2"
      and pi_amt_type <> "1"
   then
      test_qty = wqty_ext.
   /* Else Just Do It. */
   else
      test_qty = wqty_qty.

   l_test_bk_amt = 0.

   /* GET THE TOTAL AMOUNT IN VARIABLE l_test_bk_amt */
   /* FOR AMOUNT BREAKING PRICE LISTS                */

   if pi_qty_type = "2"
   then do:
      for first wqty_wkfl
         where wqty_cat = pi_break_cat
           and wqty_um  = ""
      no-lock:
         l_test_bk_amt = wqty_ext.
      end. /* FOR FIRST wqty_wkfl */
   end. /* IF pi_qty_type = "2" */

   /* If this price list is counted in a break category      */
   /* that is _not_ the normal break category for this       */
   /* item, create a reprice record for this item/break cat. */
   if pi_break_cat <> ""
   then do:

      if not available pt_mstr
         or pi_break_cat <> pt_break_cat
      then do:

         /* Check to see if this has already been added */
         find first wrep_wkfl where wrep_part = price_part
            and wrep_cat  = pi_break_cat
            and wrep_um   = test_um
         no-lock no-error.
         if not available wrep_wkfl
         then do:
            /* If not, find an un-repriced record for this */
            /* item (which should be the current line) and */
            /* add the new record.                         */
            find first wrep_wkfl where wrep_part = price_part
               and wrep_um   = test_um  /*pi_um*/
               and wrep_rep  = no
               no-lock no-error.
            if available wrep_wkfl
            then do:
               reprice_line = wrep_line.
               create wrep_wkfl.
               assign
                  wrep_line = reprice_line
                  wrep_part = price_part
                  wrep_cat  = pi_break_cat
                  wrep_um   = test_um     /*pi_um*/
                  wrep_rep  = no
                  .
            end. /* IF AVAILABLE wrep_wkfl */
         end. /* IF NOT AVAILABLE wrep_wkfl */

      end. /* IF NOT AVAILABLE pt_mstr */

   end.  /* Additional reprice rec if pi_break_cat <> pt_break_cat */

   /* Convert to absolute value of quantity */
   if test_qty < 0
   then
      test_qty = - test_qty.

   assign
      test_qty      =
                     if test_qty < 0
                     then
                        - test_qty
                     else
                        test_qty
      l_test_bk_amt =
                     if l_test_bk_amt < 0
                     then
                        - l_test_bk_amt
                     else
                        l_test_bk_amt.

   /* Make sure we aren't over max qty for list */
   if pi_max_qty <> 0 and test_qty > pi_max_qty
   then do:
      err_flag = -4.
      return.
   end. /*IF pi_max_qty <> 0 AND test_qty > pi_max_qty */

   for last pid_det
      fields(pid_amt pid_cr_terms pid_fr_list
      pid_fr_terms pid_list_id pid_qty)
      where  pid_list_id  = list_id
        and (pid_qty     <= l_test_bk_amt
         or  pid_qty     <= test_qty)
   no-lock:
   end. /*FOR LAST pid_det*/

   if not available pid_det
   then do:
      err_flag = -5.  /* Fails minimum qty criteria */
      return.
   end. /* IF NOT AVAILABLE pid_det */

   /* Discounts */
   if lookup(pi_amt_type, "2,3,4,8,9") <> 0
   then do:

      /* Determine value of this discount for comparison */

      /* Discount Percentage */
      if pi_amt_type = "2"
      then do:
         assign
            test_price = best_list_price * ( 1 - ( pid_amt / 100 ) )
            test_factor = 1 - ( pid_amt / 100 ) .
      end. /* IF pi_amt_type = "2" */

      /* Mark-up */
      else
         if pi_amt_type = "3"
         then do:

            /* Get item cost if we don't have it yet */
            /* Always get cost                       */
            /* Changed pt_site to price_site (from order/quote line) */
            if available pt_mstr
            then do:
               if pi_cost_set = ""
               then do:
                  {gprun.i ""gpsct05x.p""
                  "(pt_part, price_site, 1,
                  output glxcst, output curcst)" }
/*GUI*/ if global-beam-me-up then undo, leave.

               end. /* IF pi_cost_set = "" */
               else do:
                  {gprun.i ""gpsct07x.p""
                  "(pt_part, price_site, pi_cost_set, 1,
                  output glxcst)" }
/*GUI*/ if global-beam-me-up then undo, leave.

               end. /* ELSE DO */

               /* CONVERT CURRENCY BASE TO FOREIGN   */
               /* SINCE AMOUNT DOES NOT NEED TO BE ROUNDED   */
               /* EXISITNG CODE DOES NOT NEED TO BE MODIFIED */
               /* TO DETERMINE CURRENCY CODES USED...        */
               {gprunp.i "mcpl" "p" "mc-curr-conv"
               "(input """",
                 input """",
                 input price_exch_rate2,
                 input price_exch_rate,
                 input glxcst,
                 input false, /* DO NOT ROUND */
                 output item_cost,
                 output mc-error-number)"}.

               if item_cost = 0
               then do:
                  err_flag = -9. /*0 cost found skip this price list*/
               return.
               end. /* IF item_cost = 0 */

           end. /* IF available pt_mstr */
           else do:
              err_flag = -8. /* No pt_mstr available for markup */
              return.
           end. /* ELSE DO */

           /* Convert amounts to order UM if necessary */

           if pi_um = "" and pt_um <> price_um then
           do:
              {gprun.i ""gpumcnv.p"" "(price_um,
               pt_um,
               price_part,
               output conv)"}
/*GUI*/ if global-beam-me-up then undo, leave.

              if conv = ?
              then
                 conv = 1.
           end. /* IF PI_UM = "" AND ... */
           else
              conv = 1.

           assign
              item_cost = item_cost * conv
              test_price = item_cost * ( 1 + (pid_amt / 100) )
              test_factor =  if best_list_price <> 0
                             then
                                (( item_cost * ( 1 + (pid_amt / 100) ) )
                                / best_list_price)
                             else 1.

         end. /* IF pi_amt_type = "3" */

      /* Net price */
      else
         if pi_amt_type = "4"
         then do:

            /* Convert amounts to order UM if necessary */

            if available pt_mstr and pi_um = ""
               and pt_um <> price_um
            then do:
               {gprun.i ""gpumcnv.p""
               "(price_um, pt_um, price_part, output conv)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               if conv = ?
               then conv = 1.
            end. /* IF AVAILABLE pt_mstr AND pi_um = "" AND pt_um <> price_um */
            else
               conv = 1.

            assign
               test_price = pid_amt * conv
               test_factor = if best_list_price <> 0
                             then
                                (test_price / best_list_price)
                             else 1.

         end. /* IF pi_amt_type = "4" */

      /* Accruals (expressed as a percentage) */
      else
         if pi_amt_type = "8"
         then do:
            assign
               test_price = best_list_price * ( 1 - ( pid_amt / 100 ) )
               test_factor = 1 - ( pid_amt / 100 ) .

         end. /* IF pi_amt_type = "8" */

      /* Discount Amounts */
      else
         if pi_amt_type = "9"
         then do:

         /* Convert amounts to order UM if necessary */

         if available pt_mstr
            and pi_um = ""
            and pt_um <> price_um
         then do:
            {gprun.i ""gpumcnv.p""
            "(price_um, pt_um, price_part, output conv)"}
/*GUI*/ if global-beam-me-up then undo, leave.

            if conv = ?
            then conv = 1.
         end. /* IF AVAILABLE pt_mstr AND pi_um "" AND pt_um <> price_um */
         else
            conv = 1.

         assign
            test_price = best_list_price - pid_amt * conv
            test_factor = if best_list_price <> 0
                          then
                             (test_price / best_list_price)
                          else 1.

         end. /* IF pi_amt_type = "9" */

      /* Find existing record for comparison if this is a  */
      /* non-compatible combination type.                  */
      if pi_comb_type <> "2"
      then do:
         if lookup(pi_amt_type, "2,3,4,9") <> 0
         then do:

            /* Find old record */
            /* (If global, ignore option information;  */
            /* if not global, option info must match). */
            if not confg_price or pi_confg_disc
            then
               find first wkpi_wkfl
                  where wkpi_confg_disc = (not confg_price or
                  pi_confg_disc)
                  and wkpi_source     = "0"
                  and wkpi_srch_type  = pi_srch_type
                  and wkpi_comb_type  = pi_comb_type
                  and lookup(wkpi_amt_type, "2,3,4,9") <> 0
               exclusive-lock no-error.
            else
               find first wkpi_wkfl
                  where wkpi_parent     = price_par
                  and wkpi_feature    = price_feat
                  and wkpi_option     = price_opt
                  and wkpi_confg_disc = pi_confg_disc
                  and wkpi_source     = "0"
                  and wkpi_srch_type  = pi_srch_type
                  and wkpi_comb_type  = pi_comb_type
                  and lookup(wkpi_amt_type, "2,3,4,9") <> 0
               exclusive-lock no-error.

         end. /* IF lookup(pi_amt_type, "2,3,4,9") <> 0 */
         else do:
            /* Find old record */
            /* (If global, ignore option information;  */
            /* if not global, option info must match). */
            if not confg_price or pi_confg_disc
            then
               find first wkpi_wkfl
                  where wkpi_confg_disc = (not confg_price or
                  pi_confg_disc)
                  and wkpi_source     = "0"
                  and wkpi_srch_type  = pi_srch_type
                  and wkpi_comb_type  = pi_comb_type
                  and wkpi_amt_type   = "8"
               exclusive-lock no-error.
            else
            find first wkpi_wkfl
               where wkpi_parent     = price_par
               and wkpi_feature    = price_feat
               and wkpi_option     = price_opt
               and wkpi_confg_disc = pi_confg_disc
               and wkpi_source     = "0"
               and wkpi_srch_type  = pi_srch_type
               and wkpi_comb_type  = pi_comb_type
               and wkpi_amt_type   = "8"
            exclusive-lock no-error.

         end. /* ELSE DO */
      end. /* IF pi_comb_type <> "2" */

      if available wkpi_wkfl
         and wkpi_amt_type = "3"
      then
         l_test_price1 = wkpi_disc_amt * (1 + wkpi_amt / 100).
      else
         if available wkpi_wkfl
            and wkpi_amt_type = "4"
         then
            l_test_price1 = wkpi_amt.

      /* WHEN BEST LIST PRICE IS ZERO, FACTORS FOR NET PRICE, MARK-UP */
      /* AND DISCOUNT AMOUNT PRICE-LISTS ARE SET TO 1. TEST PRICE WILL */
      /* BE USED TO COMPARISON. */

      /* Save this factor if first of type or new discount */
      /* is better than old sys-generated discount.        */
      if not available wkpi_wkfl

         or (wkpi_source = "0" and ((test_factor < wkpi_factor) or
         (best_list_price = 0 and
         ((pi_amt_type = "9" and
         test_price > wkpi_amt ) or
         ((pi_amt_type = "3" or pi_amt_type = "4") and

         test_price < l_test_price1 )) )))
      then do:

         if available wkpi_wkfl
         then do:
            /* Create wkpi2_wkfl */
            {gppibx05.i &list=wkpi_list
               &amttype=wkpi_amt_type
               &combtype=wkpi_comb_type
               &breakcat=wkpi_break_cat
               &pidqty=wkpi_pid_qty
               &qtytype=wkpi_qty_type
               &amt=wkpi_amt
               &promo=""""
               }
            delete wkpi_wkfl.
         end. /* IF AVAILABLE wkpi_wkfl */

         create wkpi_wkfl.
         assign
            wkpi_amt         = if (pi_amt_type = "9" or
                                   pi_amt_type = "4")
                               then
                                  (pid_amt * conv)
                               else
                                  pid_amt
            wkpi_amt_type    = pi_amt_type
            wkpi_break_cat   = pi_break_cat
            wkpi_comb_type   = pi_comb_type
            wkpi_confg_disc  = (not confg_price or pi_confg_disc)
            wkpi_disc_seq    = pi_disc_seq
            wkpi_factor      = test_factor
            wkpi_feature     = price_feat
            wkpi_list        = pi_list
            wkpi_list_id     = pi_list_id
            wkpi_min_net     = pi_min_net
            wkpi_option      = price_opt
            wkpi_parent      = price_par
            wkpi_qty         = test_qty
            wkpi_pid_qty     = pid_qty
            wkpi_qty_type    = pi_qty_type
            wkpi_srch_type   = pi_srch_type
            wkpi_source      = "0"
            wkpi_um          = test_um
            .
         /* We need this cost later to calc net price */
         if wkpi_amt_type = "3"
         then
            wkpi_disc_amt = item_cost.

      end. /* IF NOT AVAILABLE wkpi_wkfl */
      else do:
         err_flag = -6.
         /* Create wkpi2_wkfl */
         {gppibx05.i &list=pi_list
            &amttype=pi_amt_type
            &combtype=pi_comb_type
            &breakcat=pi_break_cat
            &pidqty=pid_qty
            &qtytype=pi_qty_type
            &amt=pid_amt
            &promo=""""
            }
      end. /* ELSE DO */

   end.  /* Discounts */

   /* Terms and non-price discounts */
   else do:

      bad_list = no.

      /* Credit Terms */
      if pi_amt_type = "5"
      then do:

         if current_cr_terms <> ""
         then do:
            /* We are only accepting the last one (per order). */
            find first wkpi_wkfl where wkpi_amt_type = "5"
               and wkpi_feature = price_feat
               and wkpi_option  = price_opt
               and wkpi_parent  = price_par
            exclusive-lock no-error.
            if available (wkpi_wkfl)
            then do:
               /* Create wkpi2_wkfl */
               {gppibx05.i &list=wkpi_list
                &amttype=wkpi_amt_type
                &combtype=wkpi_comb_type
                &breakcat=wkpi_break_cat
                &pidqty=wkpi_pid_qty
                &qtytype=wkpi_qty_type
                &amt=wkpi_amt
                &promo=wkpi_cr_terms
               }
               delete wkpi_wkfl.
            end. /* IF AVAILABLE (wkpi_wkfl) */
         end. /* IF current_cr_terms <> "" */

         current_cr_terms = pid_cr_terms. /* for so_cr_terms later */

      end. /* IF pi_amt_type = "5" */

      /* Freight Terms */
      else
         if pi_amt_type = "6"
         then do:

            if current_fr_terms <> ""
            then do:
               /* We are only accepting the last one (per order). */
               find first wkpi_wkfl where wkpi_amt_type = "6"
                  and wkpi_feature = price_feat
                  and wkpi_option  = price_opt
                  and wkpi_parent  = price_par
               exclusive-lock no-error.
               if available (wkpi_wkfl)
               then do:
                  /* Create wkpi2_wkfl */
                  {gppibx05.i &list=wkpi_list
                   &amttype=wkpi_amt_type
                   &combtype=wkpi_comb_type
                   &breakcat=wkpi_break_cat
                   &pidqty=wkpi_pid_qty
                   &qtytype=wkpi_qty_type
                   &amt=wkpi_amt
                   &promo=wkpi_fr_terms
                  }
                  delete wkpi_wkfl.
               end. /* IF AVAILABLE (wkpi_wkfl) */
            end. /* IF current_fr_terms <> "" */
         current_fr_terms = pid_fr_terms. /* for so_fr_terms later */

         end. /* IF pi_amt_type = "6" */

      /* Freight List */
      else
         if pi_amt_type = "7"
         then do:

            /* Validate the Freight List */
            bad_list = yes.

            for first pt_mstr
               fields (pt_break_cat pt_fr_class pt_part
                       pt_pm_code pt_um)
               where pt_part = price_part no-lock:
            end. /* FOR FIRST PT_MSTR */

            if available pt_mstr
            then do:

               for first frcd_det
                  fields (frcd_class frcd_fr_list)
                  where frcd_fr_list = pid_fr_list and
                  frcd_class   = pt_fr_class no-lock:
               end. /* FOR FIRST FRCD_DET */

               if available frcd_det
               then do:

                  /* We've got a valid list */
                  bad_list = no.

                  if current_fr_list <> ""
                  then do:
                     /* We are only accepting the last one (per line). */
                     find first wkpi_wkfl where wkpi_amt_type = "7"
                        and wkpi_feature = price_feat
                        and wkpi_option  = price_opt
                        and wkpi_parent  = price_par
                     exclusive-lock no-error.
                     if available (wkpi_wkfl)
                     then do:
                        /* Create wkpi2_wkfl */
                        {gppibx05.i &list=wkpi_list
                           &amttype=wkpi_amt_type
                           &combtype=wkpi_comb_type
                           &breakcat=wkpi_break_cat
                           &pidqty=wkpi_pid_qty
                           &qtytype=wkpi_qty_type
                           &amt=wkpi_amt
                           &promo=wkpi_fr_list
                           }
                        delete wkpi_wkfl.
                     end. /* IF AVAILABLE (wkpi_wkfl) */
                  end. /* IF current_fr_list <> */
                  current_fr_list = pid_fr_list. /* for sod_fr_list */

               end. /* IF AVAILABLE frcd_det */
            end. /* IF AVAILABLE pt_mstr */
         end. /* IF pi_amt_type = "7" */

      if not bad_list
      then do:
         create wkpi_wkfl.
         assign
            wkpi_amt_type    = pi_amt_type
            wkpi_break_cat   = pi_break_cat
            wkpi_comb_type   = pi_comb_type
            wkpi_cr_terms    = if pi_amt_type = "5"
                               then
                                  current_cr_terms
                               else
                                  ""
            wkpi_disc_seq    = pi_disc_seq
            wkpi_feature     = price_feat
            wkpi_fr_list     = if pi_amt_type = "7"
            then current_fr_list
            else ""
            wkpi_fr_terms    = if pi_amt_type = "6"
            then current_fr_terms
            else ""
            wkpi_list        = pi_list
            wkpi_list_id     = pi_list_id
            wkpi_min_net     = pi_min_net
            wkpi_option      = price_opt
            wkpi_qty         = test_qty
            wkpi_qty_type    = pi_qty_type
            wkpi_srch_type   = pi_srch_type
            wkpi_source      = "0"
            wkpi_um          = test_um
            wkpi_pid_qty     = pid_qty
            .
      end. /* IF not bad_list */
   end. /* Terms Discounts */

end. /* ELSE DO */

err_flag = 0.  /* This is redundant, but it makes me feel better */
